﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Documents.Model;
using Telerik.Windows.Documents.FormatProviders;
using Telerik.Windows.Documents.FormatProviders.Txt;


namespace StatusPlan3.UC
{
    /// <summary>
    /// Interaction logic for UCMessage.xaml
    /// </summary>
    public partial class UCMessage : UserControl
    {
        public Message message;
        public UCMessages ucMessages;
        Boolean isMyReply2 = false;
        Boolean isNewMessage2 = false;
        AttachedFiles attachedFiles = new AttachedFiles();


        public UCMessage()
        {
            InitializeComponent();
        }

        public UCMessage(Message m)
        {
            InitializeComponent();
            setData(m);
        }

        public void setData(Message m)
        {
            message = m;
            TextBlock_subject.Text = m.subject;
            TextBlock_sender.Text = m.sender.fullName;
            TextBlock_date.Text = m.createdDate.ToString("dd-MM-yyyy HH:mm");

            if (m.attachedFiles.Count > 0)
            {
                Button_Attachment.Visibility = System.Windows.Visibility.Visible;

                ContextMenu cm = new ContextMenu();
                foreach (AttachedFile af in m.attachedFiles)
                {
                    MenuItem mi = new MenuItem();
                    mi.Header = af.fileName + "." + af.fileType + " (" + af.size.toMegaBytes() + " mb)";

                    MenuItem open = new MenuItem();
                    open.Header = "Lat upp";
                    open.Tag = af;
                    open.Click += new RoutedEventHandler(open_Click);
                    mi.Items.Add(open);

                    MenuItem download = new MenuItem();
                    download.Header = "Goym";
                    download.Tag = af;
                    download.Click += new RoutedEventHandler(download_Click);
                    mi.Items.Add(download);
                    cm.Items.Add(mi);
                }
                Button_Attachment.ContextMenu = cm;
                Button_Attachment.ToolTip = m.attachedFiles.Count + " fílur viðfestar";
            }

            TextBox_Message.Text = m.message;
        }

        void download_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            AttachedFile af = mi.Tag as AttachedFile;
            StatusPlanData.statusPlanHandler.downloadFile(af);
        }

        void open_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;
            AttachedFile af = mi.Tag as AttachedFile;
            StatusPlanData.statusPlanHandler.openFile(af);
        }

        public void isMessage()
        {
            TextBlock_subject.Visibility = System.Windows.Visibility.Visible;
        }

        public void isReply()
        {
               
        }

        public void isMyReply()
        {
            Grid_Subject.Visibility = System.Windows.Visibility.Collapsed;
            TextBlock_sender.Visibility = System.Windows.Visibility.Collapsed;
            TextBlock_date.Visibility = System.Windows.Visibility.Collapsed;
            Button_Reply.Visibility = System.Windows.Visibility.Visible;
            Button_Attachment.Visibility = System.Windows.Visibility.Visible;
            Button_Attachment.ContextMenu = buildAttachementContextMenu();

            isMyReply2 = true;
        }

        public void isNewMessage()
        {
            Grid_Subject.Visibility = System.Windows.Visibility.Visible;
            TextBlock_sender.Visibility = System.Windows.Visibility.Collapsed;
            TextBlock_date.Visibility = System.Windows.Visibility.Collapsed;
            Button_Reply.Visibility = System.Windows.Visibility.Visible;
            ReplyButton_Text.Text = "Send";
            Button_Attachment.Visibility = System.Windows.Visibility.Visible;
            Button_Cancel.Visibility = System.Windows.Visibility.Visible;
            isNewMessage2 = true;

            Button_Attachment.ContextMenu = buildAttachementContextMenu();
        }

        

        void mi_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Filter = "Allar fílur|*.*";
            openDialog.Multiselect = false;
            var dialogResult = openDialog.ShowDialog();

            if (dialogResult == System.Windows.Forms.DialogResult.OK)
            {
                String pathToFile = openDialog.FileName;
                String fileName = openDialog.SafeFileName;
                String fileType = fileName.Substring(fileName.LastIndexOf('.') + 1);
                fileName = fileName.Replace("." + fileType, "");
                System.IO.FileInfo f = new System.IO.FileInfo(pathToFile);

                AttachedFile af = new AttachedFile()
                {
                    createdBy = StatusPlanData.currentUser,
                    dateAdded = DateTime.Now,
                    fileName = fileName,
                    fileType = fileType,
                    size = f.Length,
                    path = pathToFile,
                };

                attachedFiles.Add(af);
            }



            Button_Attachment.ContextMenu = buildAttachementContextMenu();
        }

        private ContextMenu buildAttachementContextMenu()
        {
            ContextMenu cm = new ContextMenu();
            foreach (AttachedFile af in attachedFiles)
            {
                MenuItem mi2 = new MenuItem();
                mi2.Header = af.fileName + "." + af.fileType + " (" + af.size.toMegaBytes()  + " mb)";
                mi2.Tag = af;

                MenuItem mi3 = new MenuItem();
                mi3.Header = "Tak burtur";
                mi3.Tag = af;
                mi3.Click += new RoutedEventHandler(mi3_Click);

                mi2.Items.Add(mi3);
                cm.Items.Add(mi2);
            }

            MenuItem Split = new MenuItem();
            Split.Header = "";
            cm.Items.Add(Split);

            MenuItem mi = new MenuItem();
            mi.Header = "Viðfest fílu";
            mi.Click += new RoutedEventHandler(mi_Click);
            cm.Items.Add(mi);

            return cm;
        }

        void mi3_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = sender as MenuItem;

            AttachedFile af = mi.Tag as AttachedFile;

            attachedFiles.Remove(af);

            Button_Attachment.ContextMenu = buildAttachementContextMenu();
        }

        private void Button_Cancel_Click(object sender, RoutedEventArgs e)
        {
            TextBox_Subject.Text = "";
            TextBox_Message.Text = "";
            this.Visibility = System.Windows.Visibility.Collapsed;
            
        }

        private void Button_Reply_Click(object sender, RoutedEventArgs e)
        {
            if(TextBox_Subject.Text.Trim().Length == 0 && isNewMessage2) 
            {
                MessageBox.Show("Evni kann ikki verða tómt","Feilur: Boð",MessageBoxButton.OK,MessageBoxImage.Error);
            }
            else if (isMyReply2 && TextBox_Message.Text.Trim().Length == 0)
            {
                MessageBox.Show("Boði kann ikki verða tómt", "Feilur: Boð", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                Message m = new Message();
                if (!isNewMessage2)
                    m.subject = "Reply";
                else
                    m.subject = TextBox_Subject.Text;
                m.message = TextBox_Message.Text;
                m.createdDate = DateTime.Now;
                m.sender = StatusPlanData.currentUser;
                foreach (AttachedFile af in attachedFiles)
                    m.attachedFiles.Add(af);

                try
                {
                    if (isMyReply2)
                    {
                        if (ucMessages.project != null)
                        {
                            StatusPlanData.statusPlanHandler.replyProjectMessage(message, m, ucMessages.project.project);
                        }
                        else if (ucMessages.task != null)
                        {
                            StatusPlanData.statusPlanHandler.replyTaskMessage(message, m, ucMessages.task.task);
                            ucMessages.task.refreshFrame();
                        }
                        else if (ucMessages.issue != null)
                        {
                            StatusPlanData.statusPlanHandler.replyIssueMessage(message, m, ucMessages.issue);
                        }
                        else if (ucMessages.subproject != null)
                        {
                            StatusPlanData.statusPlanHandler.replySubprojectMessage(message, m, ucMessages.subproject);
                        }
                    }
                    else if (isNewMessage2)
                    {
                        if (ucMessages.project != null)
                        {
                            StatusPlanData.statusPlanHandler.newProjectMessage(m, ucMessages.project.project);
                        }
                        else if (ucMessages.task != null)
                        {
                            StatusPlanData.statusPlanHandler.newTaskMessage(m, ucMessages.task.task);
                            ucMessages.task.refreshFrame();
                        }
                        else if (ucMessages.issue != null)
                        {
                            StatusPlanData.statusPlanHandler.newIssueMessage(m, ucMessages.issue);
                        }
                        else if (ucMessages.subproject != null)
                        {
                            StatusPlanData.statusPlanHandler.newSubprojectMessage(m, ucMessages.subproject);
                        }
                    }
                } 
                catch(System.IO.IOException ex) 
                {

                }
                catch(Exception e2) 
                {
                    MessageBox.Show("EX: " + e2.Message + "\n\nInner: " + e2.InnerException, "Innlesing: Feilur", MessageBoxButton.OK, MessageBoxImage.Error);
                }

                MessageBox.Show("Boðini eru send", "Boð", MessageBoxButton.OK, MessageBoxImage.Information);
                Button_Cancel_Click(null, null);
            }
        }

        private void Button_Attachment_Click(object sender, RoutedEventArgs e)
        {
            Button_Attachment.ContextMenu.PlacementTarget = Button_Attachment;
            Button_Attachment.ContextMenu.IsOpen = true;
        }
    }
}
